package com.tuling.bio;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 客户端测试连接
 * telnet localhost 9000
 *  'CTRL+]'
 * send xxxx
 */
public class SocketServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9000);
        while (true) {
            System.out.println("等待连接");
            Socket clientSocket = serverSocket.accept();
            System.out.println("有客户端连接了");
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        readHandler(clientSocket);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

    public static void readHandler(Socket clientSocket) throws IOException {

        byte[] bytes = new byte[1024];
        System.out.println("准备read...");
        int read = clientSocket.getInputStream().read();
        System.out.println("read完毕");
        if (read != -1) {
            System.out.println("接受客户端数据：" + new String(bytes, 0, read));
        }
    }
}
